#include "bindings/bindings_rive_texture_view.h"
#include "helpers/general.h"
#include "models/renderer.h"
#include <string>

namespace ohos_rive {
napi_value RiveTextureViewGetAverageFps(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1];

    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok || argc < 1) {
        LOGE("Invalid arguments - expected renderer reference");
        return nullptr;
    }

    // 获取渲染器指针
    int64_t rendererRef;
    status = napi_get_value_int64(env, args[0], &rendererRef);
    if (status != napi_ok) {
        LOGE("Failed to get renderer reference");
        return nullptr;
    }
    auto renderer = reinterpret_cast<Renderer *>(rendererRef);
    if (!renderer) {
        LOGE("Invalid renderer reference");
        return nullptr;
    }

    // 获取平均FPS
    float fps = renderer->AverageFps();

    napi_value result;
    status = napi_create_double(env, fps, &result);
    if (status != napi_ok) {
        LOGE("Failed to create result");
        return nullptr;
    }
    LOGI("Average FPS: %{public}f", fps);
    return result;
}

} // namespace ohos_rive